package com.woniuxy.exam.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.commons.dto.QuestionDto;
import com.woniuxy.commons.entity.MultipleChoice;
import com.woniuxy.commons.entity.ResponseEntity;
import com.woniuxy.commons.utils.DataTypeUtils;
import com.woniuxy.commons.utils.TimeUtils;
import com.woniuxy.exam.service.MultipleChoiceService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.util.List;

/**
 * @author zouyang
 * @date 2020/7/17 - 23:30
 */
@RestController
public class MultipleChoiceController {

    @Autowired
    private MultipleChoiceService multipleChoiceService;

    @RequestMapping("/mc/all")
    public List<MultipleChoice> getAll(){
        return multipleChoiceService.selectAll();
    }
    @RequestMapping("/mc/search")
    public List<MultipleChoice> search(QuestionDto questionDto){
        MultipleChoice multipleChoice = new MultipleChoice();
        BeanUtils.copyProperties(questionDto,multipleChoice);
        return multipleChoiceService.selectByAll(multipleChoice);
    }

    @RequestMapping("/mc/add")
    public ResponseEntity add(@RequestBody MultipleChoice multipleChoice){

        multipleChoice.setAnswer(DataTypeUtils.arrayToString(multipleChoice.getAnswerArray().toArray()));
        multipleChoice.setCreateDate(TimeUtils.getNowTime());
        int res = multipleChoiceService.insert(multipleChoice);
        if(res!=1){
            return new ResponseEntity(500,"系统繁忙");
        }
        return ResponseEntity.SUCCESS_RESPONSE;
    }

    @GetMapping("/mc/find/{id}")
    public MultipleChoice find(@PathVariable("id") int id){
        return multipleChoiceService.selectByPrimaryKey(id);
    }

    @PostMapping("/mc/caps")
    public ResponseEntity conditionAndPageSearch(QuestionDto questionDto){
        MultipleChoice multipleChoice = new MultipleChoice();
        BeanUtils.copyProperties(questionDto,multipleChoice);
        PageHelper.startPage(questionDto.getPageNum(),questionDto.getPageSize());
        List<MultipleChoice> list =  multipleChoiceService.selectByAll(multipleChoice);
        PageInfo pageInfo = new PageInfo(list,10);
        return new ResponseEntity(200,"success",pageInfo);
    }

    @RequestMapping("/mc/update")
    public ResponseEntity update(@RequestBody MultipleChoice multipleChoice){
        int res = multipleChoiceService.updateByPrimaryKeySelective(multipleChoice);
        if(res!=1){
            return new ResponseEntity(500,"系统繁忙");
        }
        return ResponseEntity.SUCCESS_RESPONSE;
    }

    @RequestMapping("/mc/delete")
    public ResponseEntity delete(int id){
        int res = multipleChoiceService.deleteByPrimaryKey(id);
        if(res!=1){
            return new ResponseEntity(500,"出现异常");
        }
        return new ResponseEntity(200,"success");
    }
    
}
